package com.seeyon.apps.kk.manager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.axis2.transport.http.HTTPConstants;
import org.apache.axis2.transport.http.HttpTransportProperties;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.locknlock.sd.ifsd050.IFSD050_CN_GW_SOServiceStub;
import com.locknlock.sd.ifsd050.IFSD050_CN_GW_SOServiceStub.IFSD050_CN_GW_REQ_DT;
import com.locknlock.sd.ifsd050.IFSD050_CN_GW_SOServiceStub.IFSD050_CN_GW_REQ_MT;
import com.locknlock.sd.ifsd050.IFSD050_CN_GW_SOServiceStub.IFSD050_CN_GW_RES_MT;
import com.locknlock.sd.ifsd050.IFSD050_CN_GW_SOServiceStub.ITEM_type0;
import com.locknlock.sd.ifsd050.IFSD050_CN_GW_SOServiceStub.Return_type0;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.kk.utils.ValueUtils;
import com.seeyon.v3x.services.form.FormFactory;
import com.seeyon.v3x.services.form.FormUtils;
import com.seeyon.v3x.services.form.bean.FormExport;

public class LockKehuEduManager extends BaseManager {
	private static final Log log = LogFactory.getLog(LockKehuEduManager.class);
	private FreemarkFlowManager freemarkFlowManager;

	public FreemarkFlowManager getFreemarkFlowManager() {
		return freemarkFlowManager;
	}

	public void setFreemarkFlowManager(FreemarkFlowManager freemarkFlowManager) {
		this.freemarkFlowManager = freemarkFlowManager;
	}

	public void addKehuEduForm(Long id) {
		String findallsql = this.freemarkFlowManager.fltTemplateIntoString("sql/sap/sendtosapsql/FIND_ALL_EDU.ftl",
				null);
		List<Map<String, Object>> queryForList = this.jdbcTemplate.queryForList(findallsql, id);
		for (Map<String, Object> map : queryForList) {
			try {
				this.addEaLog(map);
				this.sendToSap(map);
				this.UpdateLog(map);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public Map<String, Object> sendToSap(Map<String, Object> map) throws Exception {
		if (!map.isEmpty()) {
			IFSD050_CN_GW_SOServiceStub ifsd = new IFSD050_CN_GW_SOServiceStub();
			HttpTransportProperties.Authenticator auth = new HttpTransportProperties.Authenticator();
			auth.setUsername("IFPO01");
			auth.setPassword("Passpo1$");
			ifsd._getServiceClient().getOptions().setProperty(HTTPConstants.AUTHENTICATE, auth);
			IFSD050_CN_GW_REQ_MT iFSD050_CN_GW_REQ_MT0 = new IFSD050_CN_GW_REQ_MT();
			IFSD050_CN_GW_REQ_DT param = new IFSD050_CN_GW_REQ_DT();
			ITEM_type0[] sds050 = new ITEM_type0[map.size()];
			int num = 0;
			ITEM_type0 aa = new ITEM_type0();
			aa.setI_KUNNR(MapUtils.getString(map, "客户编号"));
			aa.setI_LIMIT_VALID_DATE(MapUtils.getString(map, "有效日期"));
			aa.setI_NETWR(MapUtils.getString(map, "金额"));// 信贷限额
			aa.setI_RISK_CLASS(MapUtils.getString(map, "风险类型"));
			aa.setI_VKBUR(MapUtils.getString(map, "营业组织"));
			aa.setI_VKORG(MapUtils.getString(map, "销售办事处"));
			aa.setI_VKGRP("");
			sds050[num++] = aa;
			param.setITEM(aa);
			iFSD050_CN_GW_REQ_MT0.setIFSD050_CN_GW_REQ_MT(param);

			IFSD050_CN_GW_RES_MT ifsd050_CN_GW_SO = ifsd.IFSD050_CN_GW_SO(iFSD050_CN_GW_REQ_MT0);
			Return_type0 return1 = ifsd050_CN_GW_SO.getIFSD050_CN_GW_RES_MT().getReturn();
			System.out.println(return1.getE_MESSAGE());
			System.out.println(return1.getE_MSGTYP());
			map.put("XIAOXI", return1.getE_MESSAGE());
			map.put("STATUS", return1.getE_MSGTYP());
		}
		return map;
	}

	public void addEaLog(Map<String, Object> map) {
		map.put("types", "客户额度申请");// 推送类型
		String selectYsj = this.freemarkFlowManager.fltTemplateIntoString("sql/sap/ealog/kehuedu.ftl", map);
		map.put("source", selectYsj);// 推送源数据
		map.put("agin", ValueUtils.getConfig().getLong("kk.CXCS"));// 是否重新传输
		long mostSignificantBits = UUID.randomUUID().getMostSignificantBits();
		map.put("SUIJI", mostSignificantBits);
		// 生成新的日志记录
		String dataXml = this.freemarkFlowManager.fltTemplateIntoString("sql/sap/ealog/EALOG.ftl", map); // memberDay可以直接给
		// 可以使用数据模板生成，这样底表事件依然有效
		FormFactory formFactory = (FormFactory) AppContext.getBean("formFactory");
		try {
			FormExport[] formExportData = FormUtils.xmlTransformFormExports(dataXml);
			for (FormExport formExport : formExportData) {
				formFactory.importBusinessFormData("admin1", "LK_SAPLOG_51", formExport, null);
			}
		} catch (Exception e) {
			log.error(e);
		}
	}
	
	public void UpdateLog(Map<String, Object> map) {
		Map<String,Object> msp = new HashMap<>();
		msp.put("STATUS", MapUtils.getString(map, "STATUS"));
		msp.put("MESSAGER", MapUtils.getString(map, "XIAOXI"));
		msp.put("SUI", MapUtils.getString(map, "SUIJI"));
		String updatesql = this.freemarkFlowManager.fltTemplateIntoString("sql/sap/ealog/gengxin.ftl", msp);
		this.jdbcTemplate.update(updatesql);
	}
}
